package com.leetcode.demo;

/**
 * @author wangxiyuan
 * @description 给你两个二进制字符串，返回它们的和（用二进制表示）。输入为 非空 字符串且只包含数字 1 和 0。
 * 输入: a = "11", b = "1"
 * 输出: "100"
 *
 * 输入: a = "1010", b = "1011"
 * 输出: "10101"
 * @createDate 2021/8/27 16:16
 *
 * TODO 位运算解题
 **/
public class Question67 {
    public String addBinary(String a, String b) {
        return to2(transfer(a) + transfer(b));
    }

    public String addBinary2(String a, String b) {
        int dis = Math.abs(a.length()-b.length());
        StringBuilder sb = new StringBuilder();
        // 高位补0
        for (int i = 0; i < dis; i++) {
            sb.append("0");
        }
        if(a.length() > b.length()){
            b = sb.append(b).toString();
        }else{
            a = sb.append(a).toString();
        }
        sb = new StringBuilder();
        int sum = 0;
        for (int i = a.length() - 1; i >= 0 ; i--) {
            // sum 取值范围0，1，2，3
            sum += charToInt (a.charAt (i)) + charToInt (b.charAt (i));
            sb.append(sum % 2);
            // 需要进位
            if(sum / 2 == 1){
                sum = 1;
            }else{
                sum = 0;
            }
        }
        // 如果首位需要进位的话
        if(sum == 1){
            sb.append(sum);
        }
        return sb.reverse().toString();
    }

    public int charToInt(char content){
        return content - '0';
    }

    public long transfer(String str){
        long sum = 0;
        for (int i = str.length() - 1; i >=0 ; i--) {
            if(str.charAt(i) == '1'){
                sum += 1 << (str.length() - i - 1);
            }
        }
        return sum;
    }

    public String to2(long number){
        StringBuilder stringBuilder = new StringBuilder();
        while (number != 0){
            stringBuilder.append(number % 2);
            number = number / 2;
        }
        String string = stringBuilder.reverse().toString();
        return "".equals(string) ? "0" : string;
    }

    public static void main(String[] args) {
        System.out.println ('1' - '0');
        Question67 question67 = new Question67();
        System.out.println(question67.transfer("10100000100100110110010000010101111011011001101110111111111101000000101111001110001111100001101"));
        // 超出long长度范围了
        System.out.println(question67.transfer("110101001011101110001111100110001010100001101011101010000011011011001011101111001100000011011110011"));
        //"000010100000100100110110010000010101111011011001101110111111111101000000101111001110001111100001101"
        //"110101001011101110001111100110001010100001101011101010000011011011001011101111001100000011011110011"
        //"110111101100010011000101110110100000011101000101011001000011011000001100011110011010010011000000000"
        System.out.println(question67.addBinary2("10100000100100110110010000010101111011011001101110111111111101000000101111001110001111100001101"
                ,"110101001011101110001111100110001010100001101011101010000011011011001011101111001100000011011110011"));
    }
}
